package com.zero.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zero.entity.Email;
import com.zero.mapper.EmailMapper;
import com.zero.mapper.FamilyMemberMapper;
import com.zero.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class EmailService {

    @Autowired
    private EmailMapper emailMapper;

    @Autowired
    private FamilyMemberMapper familyMemberMapper;

    /**
     * 获取邮件信息
     * @param email
     * @return
     */
    public Result getEmail(Email email) {
        if (email == null)
            return Result.fail("获取不到分页数据");

        //设置分页信息
        if (email.getPage() != null ){
            if (email.getLimit() != null)
                PageHelper.startPage(email.getPage(), email.getLimit());
            else
                PageHelper.startPage(email.getPage(), 10);
        }else{
            PageHelper.startPage(1, 10);
        }

        List<Email> result = emailMapper.select(email);

        for (Email temp : result) {
            temp.setFamilyMember(familyMemberMapper.selectById(temp.getMemberId()));
        }
        PageInfo pageInfo = new PageInfo(result);
        return Result.ok(result, pageInfo.getTotal());
    }

    /**
     * 添加邮件
     * @param email
     * @return
     */
    public Result addEmail(Email email){
        if (email == null)
            return Result.fail("邮件信息不完整");
        email.setStatus(0);
        email.setCreateDate(new Date());
        int row = emailMapper.add(email);
        if(row == 1)
            return Result.ok("邮件发送成功");
        return Result.fail("邮件发送失败");
    }

    /**
     * 删除邮件
     * @param ids
     * @return
     */
    @Transactional
    public Result deleteEmail(String ids){

        if (ids == null || ids.equals(""))
            return Result.fail("删除失败");
        String[] idSet = ids.split(",");
        int row = 0;
        for (String id : idSet) {
            row += emailMapper.delete(Integer.parseInt(id));
        }

        return Result.ok("成功删除"+ row + "条记录");
    }

    /**
     * 标记邮件
     * @param id
     * @return
     */
    public Result markEmail(int id){
        Email email = new Email();
        email.setId(id);
        email.setStatus(1);
        int row = emailMapper.update(email);
        if(row == 1)
            return Result.ok("操作成功");
        return Result.fail("操作失败");
    }


    /**
     * 获取最近3条邮件反馈，以及未处理的反馈数量
     * @return
     */
    public Result getRecentEmail() {
        Email email = new Email();
        List<Email> result = emailMapper.select(email);
        int count = 0;
        for (Email temp : result) {
            if (temp.getStatus() == 0)
                count++;
        }

        if (result.size() <= 3)
            return Result.ok(result, count);
        return Result.ok(result.subList(0,3), count);
    }
}
